package com.j.lemon.learn.thread;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author lijunjun
 * @Date 2019-03-20 15:05
 * @Description
 */
public class ConcurrentHashMapTest {
    private static final ConcurrentHashMap<String,String> CONCURRENT_HASH_MAP = new ConcurrentHashMap<>();
    private static final HashMap<String,String> HASH_MAP = new HashMap<>();
    private static final ThreadPoolExecutor THREAD_POOL = new ThreadPoolExecutor(8, 16, 0, TimeUnit.MILLISECONDS,
            new LinkedBlockingDeque<>(), r -> new Thread(r,"hahaha"),new ThreadPoolExecutor.DiscardPolicy());

    static class MyTest implements Runnable{
        private String f;
        MyTest(String f) {
            this.f=f;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10000; i++) {
                HASH_MAP.put(f+i,String.valueOf(i));
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        THREAD_POOL.execute(new MyTest("a"));
        THREAD_POOL.execute(new MyTest("b"));
        THREAD_POOL.execute(new MyTest("c"));
        THREAD_POOL.shutdown();
        Thread.sleep(3000);

        System.out.println(HASH_MAP.size());
    }
}
